package com.djzhou.gmms.service.user.impl;

import java.util.HashMap;
import java.util.List;

import org.springframework.stereotype.Service;

import com.djzhou.gmms.model.entity.PageBean;
import com.djzhou.gmms.model.role.TbRole;
import com.djzhou.gmms.model.user.TbUserRole;
import com.djzhou.gmms.model.user.VoResource;
import com.djzhou.gmms.service.base.impl.BaseServiceImpl;
import com.djzhou.gmms.service.user.UserRoleService;

public class UserRoleServiceImpl  extends BaseServiceImpl implements UserRoleService{

	@Override
	public void add(TbUserRole t) {
		userRoleDAO.insert(t);
		
	}

	@Override
	public void delete(Integer id) {
		String hql="delete from TbUserRole ur where ur.userId=?";
		userRoleDAO.delete(hql, id);
	}

	@Override
	public TbUserRole queryById(Integer id) {
		
		return null;
	}

	@Override
	public List<TbUserRole> queryList(HashMap<String, Object> hashMap) {
		String hql="from TbUserRole ur where ur.roleId=?";
		List<TbUserRole> list=userRoleDAO.query(hql, Integer.parseInt(hashMap.get("roleId").toString()));
		return list;
	}

	@Override
	public PageBean<TbUserRole> queryPageBean(HashMap<String, Object> hashMap,
			int pageSize, int currentPage) {
		
		return null;
	}

	@Override
	public void update(TbUserRole t) {
		
	}

	@Override
	public VoResource query4AutoComplete(HashMap<String, Object> hashMap, String sidx,
			String sord, int pageSize, int currentPage) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void add(String roleIds, Integer userId,Integer mainRoleId) {
		if(roleIds.indexOf(",")!=-1){
			String[] array=roleIds.split(",");
			for(String s:array){
				TbUserRole userRole=new TbUserRole();
				Integer roleId=Integer.parseInt(s.trim());
				if(roleId.equals(mainRoleId))userRole.setMainRoleFlag(1);
				else userRole.setMainRoleFlag(0);
				userRole.setRoleId(roleId);
				userRole.setUserId(userId);
				userRoleDAO.insert(userRole);
			}
		}else{
			Integer roleId=Integer.parseInt(roleIds);
			TbUserRole userRole=new TbUserRole();
			userRole.setMainRoleFlag(1);
			userRole.setRoleId(roleId);
			userRole.setUserId(userId);
			userRoleDAO.insert(userRole);
		}
		
	}

	@Override
	public void update(String roleIds, Integer userId,Integer mainRoleId) {
		String hql="delete from TbUserRole ur where ur.userId=?";
		userRoleDAO.delete(hql, userId);
		
		if(roleIds.indexOf(",")!=-1){
			String[] array=roleIds.split(",");
			for(String s:array){
				TbUserRole userRole=new TbUserRole();
				Integer roleId=Integer.parseInt(s.trim());
				if(roleId.equals(mainRoleId))userRole.setMainRoleFlag(1);
				else userRole.setMainRoleFlag(0);
				userRole.setRoleId(roleId);
				userRole.setUserId(userId);
				userRoleDAO.insert(userRole);
			}
		}else{
			Integer roleId=Integer.parseInt(roleIds);
			TbUserRole userRole=new TbUserRole();
			userRole.setMainRoleFlag(1);
			userRole.setRoleId(roleId);
			userRole.setUserId(userId);
			userRoleDAO.insert(userRole);
		}
	}

	@Override
	public String queryRoleIdsByUserId(Integer userId) {
		String hql="from TbUserRole ur where ur.userId=?";
		List<TbUserRole> list=userRoleDAO.query(hql, userId);
		String str=",";
		for(TbUserRole ur:list){
			str+=ur.getRoleId()+",";
		}
		return str;
	}

	@Override
	public List<TbRole> queryRoleListByUserId(Integer userId) {
		String hql="from TbUserRole ur where ur.userId=?";
		List<TbUserRole> list=userRoleDAO.query(hql, userId);
		String str="";
		for(TbUserRole ur:list){
			str+=ur.getRoleId()+",";
		}
		if(str.equals(""))
			str="-1";
		else
			str=str.substring(0,str.length()-1);
		String hql2="from TbRole role where role.roleId in("+str+")";
		return roleDAO.query(hql2);
	}

	@Override
	public String queryRoleNamesByUserId(Integer userId) {
		String hql="from TbUserRole ur where ur.userId=? and ur.mainRoleFlag=1";
		List<TbUserRole> list=userRoleDAO.query(hql, userId);
		String str="";
		str=roleDAO.queryByID(TbRole.class, list.get(0).getRoleId()).getRoleName();
		return str;
	}

	@Override
	public TbRole queryMainRoleByUserId(Integer userId) {
		String hql="from TbUserRole ur where ur.userId=? and ur.mainRoleFlag=1";
		List<TbUserRole> list=userRoleDAO.query(hql, userId);
		if(list.size()>0)
			return roleDAO.queryByID(TbRole.class, list.get(0).getRoleId());
		else
			return null;
	}

}
